using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace tranquility
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    /// 

    //public enum BatState { None, Grounded } 

    public class Bat : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public static Texture2D SpriteSheet { get; private set; }
        SpriteBatch spriteBatch;
        public Vector3 Velocity;
        public Rectangle initRect;
        public Vector3 Position;
        public Vector3 actPositionLeft;
        public Vector3 actPositionRight;

        double angle = 0;
        public BasicEffect basicEffect;

        AnimatedSprite Sprite;

        Game game;

        public Bat(Game g, Vector3 initialPosition, Vector3 initVelocity, SpriteBatch sb)
            : base(g)
        {
            actPositionLeft = initialPosition + new Vector3(-300, 5, 0);
            actPositionRight = initialPosition + new Vector3(300, 5, 0);

            Position = initialPosition;
            Velocity = initVelocity;
            spriteBatch = sb;
            game = g;

        }

        protected override void LoadContent()
        {
            Sprite = new AnimatedSprite(Game.Content.Load<Texture2D>("Bat"), 1, 32, 31);
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Matrix projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
            Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);

            basicEffect = new BasicEffect(GraphicsDevice);

            basicEffect.World = Matrix.Identity;
            basicEffect.View = Matrix.Identity;
            basicEffect.Projection = halfPixelOffset * projection;

            basicEffect.TextureEnabled = true;
            basicEffect.VertexColorEnabled = true;

            spriteBatch.Begin(0, null, null, null, null, basicEffect);

            spriteBatch.Draw(Sprite.Texture, new Vector2(Position.X, Position.Y), Sprite.SourceRect, Color.White, 0f, Sprite.Origin, 1.0f, SpriteEffects.None, Position.Z);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        public void Cohere()
        {
        }

        public void Align()
        {
        }

        public void Separate()
        {
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        float speed = 100;

        public override void Update(GameTime gameTime)
        {
            // how many sin waves should the object move through while going across the screen once
            float waves = 3;

            // distance(px) that the object moves over the course of a sin wave
            float waveHeight = 50;

            Position.X += (float)(gameTime.ElapsedGameTime.Milliseconds / 1000.0) * speed;
            Position.Y = 100 + (float)Math.Sin((Position.X / GraphicsDevice.Viewport.Width) * waves * 2 * Math.PI) * waveHeight;

            if (Position.X > GraphicsDevice.Viewport.Width || Position.X < 0)
                speed = -speed;

            Sprite.HandleSpriteMovement(gameTime);
            Sprite.AnimateForward(gameTime);

            base.Update(gameTime);
        }
    }
}
